// 求解无符号数a的23次方，四个时钟算完

//流水线写法
module top1(
           input clk,
           input rst_n,
           input [2: 0] a,
           output reg [68: 0] result
       );

reg [5: 0] result_2;
reg [14: 0] result_5;
reg [32: 0] result_11;
always@(posedge clk or negedge rst_n)
	begin
		if (!rst_n)
			begin
				result_2 <= 69'd0;
				result_5 <= 69'd0;
				result_11 <= 69'd0;
				result <= 69'd0;
			end
		else
			begin
				result_2 <= a * a;
				result_5 <= result_2 * result_2 * a;
				result_11 <= result_5 * result_5 * a;
				result <= result_11 * result_11 * a;
			end
	end

endmodule


//复用写法
module top2(
    input clk,
    input rst_n,
    input [2: 0] a,
    input a_en,
    output wire [68: 0] result
);

reg [68: 0] result_r;
reg a_en_r, a_en_rr;

wire pos_flg;
wire neg_flg;

always@(posedge clk or negedge rst_n)
	begin
		if (!rst_n)
			begin
				{a_en_r, a_en_rr} <= 2'b0;
			end
		else
			begin
				{a_en_rr, a_en_r} <= {a_en_r, a_en};
			end
	end

// a_en保证4个周期为高电平
always@(posedge clk or negedge rst_n)
	begin
		if (!rst_n)
			result_r <= 0;
		else if (pos_flg)
			result_r <= a * a;
		else if (neg_flg)
			result_r <= 'd0;
		else
			result_r <= result_r * result_r * a;
	end

assign pos_flg = (~a_en_rr && a_en_r) ? 1'b1 : 1'b0;
assign neg_flg = (~a_en_r && a_en_rr) ? 1'b1 : 1'b0;

endmodule
